package com.ruoyi.faceApi.thrift;

import java.io.*;

/**
 * 
 * 
 * @author
 *
 */
public class FileUtil {
	/**
	 * 获得指定文件的byte数组
	 * 
	 * @param filePath
	 * @return
	 * @throws Exception
	 */
	public static byte[] getBytes(String filePath) throws Exception {
		byte[] buffer = null;
		FileInputStream fis = null;
		ByteArrayOutputStream bos = null;
		try {
			int len = 1024;
			File file = new File(filePath);
			fis = new FileInputStream(file);
			bos = new ByteArrayOutputStream(len);
			byte[] b = new byte[len];
			int n;
			while ((n = fis.read(b)) != -1) {
				bos.write(b, 0, n);
			}
			fis.close();
			bos.close();
			buffer = bos.toByteArray();
		} catch (Exception e) {
			throw e;
		} finally {
			if (fis != null) {
				fis.close();
			}
			if (bos != null) {
				bos.close();
			}
		}
		return buffer;
	}

	/**
	 * 根据byte数组，生成文
	 * 
	 * @param bytes
	 * @param filePath
	 * @throws Exception
	 */
	public static void createFile(byte[] bytes, String filePath) throws Exception {
		BufferedOutputStream bos = null;
		FileOutputStream fos = null;
		File file = null;
		try {
			File dir = new File(filePath);
			if (!dir.exists()) {
				dir.getParentFile().mkdirs();
			}
			file = new File(filePath);
			fos = new FileOutputStream(file);
			bos = new BufferedOutputStream(fos);
			bos.write(bytes);
		} catch (Exception e) {
			throw e;
		} finally {
			if (bos != null) {
				bos.close();
			}
			if (fos != null) {
				fos.close();
			}
		}
	}

	public static boolean deleteFile(String sPath) {
		boolean flag = false;
		File file = new File(sPath);
		// 路径为文件且不为空则进行删除
		if (file.isFile() && file.exists()) {
			try {
				file.delete();
				flag = true;
			} catch (Exception ex) {
				System.out.println("删除文件时出现错误");
			}
		}
		return flag;
	}
}